<template>
  <div>
    <el-card class="box-card">
      <div>
        <div slot="header" class="clearfix">
        <el-button
          type="primary"
          style="float: left; padding: 10px 10px"
          @click="create_role"
          >创建角色</el-button
        >
      </div>
      </div>
      <template>
        <el-table :data="list" border style="width: 100%">
          <el-table-column prop="name" label="角色名称" width="180">
          </el-table-column>
          <el-table-column prop="create_time" label="创建时间" width="180">
              <template slot-scope="scope">
                    {{new Date(scope.row.create_time).toLocaleString()}}
                </template>
          </el-table-column>
          <el-table-column prop="auth_time" label="授权时间"> 
              <template slot-scope="scope">
                    {{scope.row.auth_time? new Date(scope.row.auth_time).toLocaleString():""}}
                </template>
          </el-table-column>
          <el-table-column prop="auth_name" label="授权人">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="openAddUserBox(scope.row)"
                >设置权限</el-button
              >
            
            </template>
          </el-table-column>
        </el-table>
      </template>
      <div style="text-align: center">
        <el-pagination
          class="pagination"
          background
          layout="prev, pager, next"
          :total="total"
          @current-change="current_change"
          :page-size="pageSize"
          @next-click="handlenext"
          @prev-click="handleprev"
        >
        </el-pagination>
      </div>
      <el-dialog title="设置权限" :visible.sync="dialogVisible">
            <div style="text-align:center">
                <el-tag>当前设置角色:{{setName}}</el-tag>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-tree :data="CheckRoleData" show-checkbox node-key="id" 
                         ref="tree">
                </el-tree>
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary">确 定</el-button>
            </div>
        </el-dialog>
      
    </el-card>
  </div>
</template>
<script>
import { getrole,addrole } from "../api/index";
export default {
  data() {
    return {
      user: {},
      list: [],
      loading: false, //加载动画
      parentId: 0,
      pageNum: 1,
      pageSize: 5,
      total: 0,
      name: "",
      dialogVisible:false,
      setName:'',
      CheckRoleData:[],
      

      
      
    };
  },
  async mounted() {
    let { data } = await getrole({
      pageNum: this.pageNum,
      pageSize: this.pageSize,
    });
    this.user = data.data;
    this.list = data.data.list;
    this.total = data.data.total;
    

  },
  methods: {
    current_change(val) {
      this.getrole(val);
    },
    async getrole(val) {
      let { data } = await getrole({
        pageNum: val,
        pageSize: this.pageSize,
      });
      this.user = data.data;
      this.list = data.data.list;
      this.total = data.data.total;
    },
    async addrole(val) {
      await addrole({
        roleName: val,
      });
      
    },
    handlenext(val) {
      this.getrole(val);
    },
    handleprev(val) {
      this.getrole(val);
    },
    create_role() {
      this.$prompt("请输入角色名", "添加角色", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          if (!value) {
            this.$message({
              type: "error",
              message: "不能为空",
            });
            return;
          }
          this.$message({
            type: "success",
            message: "添加成功！",
          });
            this.addrole(value)
        })
        .catch(() => {});
    },
    openAddUserBox(row){
      this.dialogVisible=true
      console.log(row);
    }
  },
 
};
</script>